package me.lc.superDown.layer;

import me.lc.superDown.R;
import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameMap;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.scene.GameScene;
import me.lc.superDown.sprite.ClockBar;
import me.lc.superDown.sprite.CoinBar;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.utils.ResolutionIndependent;

public class GameLayer extends Layer {

	public GameLayer() {
		setAccelerometerEnabled(true);
		clockBar = new ClockBar();
		clockBar.setPosition(ResolutionIndependent.resolveDp(10),
				GameSystem.Screen_Height - ResolutionIndependent.resolveDp(10)
						- GameSystem.tex_clock.getHeight() / 2);
		clockBar.setAnchorX(0);

		coinBar = new CoinBar();
		coinBar.setPosition(ResolutionIndependent.resolveDp(10),
				GameSystem.Screen_Height - ResolutionIndependent.resolveDp(10)
						- GameSystem.tex_clock.getHeight() * 1.5f);
		coinBar.setAnchorX(0);

		btn_pause = Button.make(R.drawable.button_pause,
				R.drawable.button_pause_selected, this, "onPauseClicked");
		btn_pause.setAnchor(1, 1);
		btn_pause.setPosition(
				GameSystem.Screen_Width - ResolutionIndependent.resolveDp(8),
				GameSystem.Screen_Height - ResolutionIndependent.resolveDp(8));
	}

	public ClockBar clockBar;
	public CoinBar coinBar;
	public Button btn_pause;

	public void onPauseClicked() {
		if (GameManager.instance.isStarted && !GameManager.instance.isPaused) {
			GameManager.instance.isPaused = true;
			GameMap.instance.standGuy();
			GameScene.make().pauseLayer.setVisible(true);
			GameScene.make().layer.btn_pause.setVisible(false);
		}
	}

	@Override
	public void wyAccelerometerChanged(float accelX, float accelY, float accelZ) {
		if (GameManager.instance.isStarted && !GameManager.instance.isPaused) {
			if (accelX != 0 && Math.abs(accelX) > 0.1) {
				float x = accelX * GameSystem.RunSpeed;
				if (x > GameSystem.MaxMoveUnit) {
					x = GameSystem.MaxMoveUnit;
				}
				GameMap.instance.moveMan(x);
			} else {
				GameMap.instance.standGuy();
				GameMap.instance.setJumpNum(accelX * GameSystem.RunSpeed);
			}
		}
	}
}
